#include<iostream>
#include<csignal>
#include<unistd.h>
#include <sys/types.h>

using namespace std;

void handler(int sig) {
    printf("Caught signal %d\n", sig);
}

int main()
{
    // 注册信号处理函数
    // signal(SIGINT, handler);      // 2号信号
    // signal(40, handler);          // 40号信号（实时信号）

    sigset_t set;
    sigset_t oset;
    sigemptyset(&set);
    sigemptyset(&oset);
    sigaddset(&set,40);
    sigaddset(&set,2);

    struct sigaction sig;
    sig.sa_handler=handler;
    sig.sa_flags=0;
    sigemptyset(&sig.sa_mask);

    sigaction(2,&sig,NULL);
    sigaction(40,&sig,NULL);

    //对信号进行阻塞
    sigprocmask(SIG_BLOCK,&set,&oset);
    cout<<"阻塞了信号2，40"<<endl;



    int cnt = 30;
    while(cnt) 
    {
        cout<<"process is :"<<getpid()<<endl;
        cout<<"begin cnt : "<<cnt--<<endl;
        sleep(1);
    }


    sigprocmask(SIG_UNBLOCK,&set,NULL);

    cnt=10;
    while(cnt) 
    {
        cout<<"process is :"<<getpid()<<endl;
        cout<<"after cnt : "<<cnt--<<endl;
        sleep(1);
    }

    return 0;
}
